﻿using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Unity.Collections;

public static class GpuHudUtility
{
    private static List<object> antiGCCollect = new List<object>(100);
    private static NativeList<int> removedAntiGCObject;
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static int HookObject(object obj)
    {
        if (removedAntiGCObject.IsCreated && removedAntiGCObject.Length > 0)
        {
            int value = removedAntiGCObject[removedAntiGCObject.Length - 1];
            removedAntiGCObject.RemoveAt(removedAntiGCObject.Length - 1);
            antiGCCollect[value] = obj;
            return value;
        }
        antiGCCollect.Add(obj);
        return antiGCCollect.Count - 1;
    }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static void RemoveHookedObject(int index)
    {
        antiGCCollect[index] = null;
        if (!removedAntiGCObject.IsCreated) removedAntiGCObject = new NativeList<int>(50, Allocator.Persistent);
        removedAntiGCObject.Add(index);
    }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static object GetHookedObject(int index)
    {
        return antiGCCollect[index];
    }

    public static T GetHookedObject<T>(int index) where T : class
    {
        return GetHookedObject(index) as T;
    }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static void SetHookedObject(int index, object obj)
    {
        antiGCCollect[index] = obj;
    }

    public static void DisposeHook()
    {
        if (removedAntiGCObject.IsCreated)
        {
            removedAntiGCObject.Dispose();
        }
    }
}